//
//  ZxFmAddReadViewController.m
//  XiMaFM
//
//  Created by fanzhuxian on 2017/9/8.
//  Copyright © 2017年 hcios. All rights reserved.
//

#import "ZxFmAddReadViewController.h"

#import "ZXReadAddTableViewCell.h"
#import "ZXReadAddViewModel.h"
#import "CXRefreshStateHeader.h"

@interface ZxFmAddReadViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) ZXReadAddViewModel * model;

@property (nonatomic, strong) UITableView * tableView;

@end

@implementation ZxFmAddReadViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.tableView.mj_header beginRefreshing];
}

#pragma mark - tableView DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.model.pageId;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.model.model.pageSize;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    ZXReadAddTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"RecomSubCell" forIndexPath:indexPath];
    [cell.albumCoverImgView sd_setImageWithURL:[self.model coverURLForIndex:indexPath.row] placeholderImage:[UIImage imageNamed:@"cell_bg_niData_2"]];
    cell.titleLabel.text = [self.model titleForIndex:indexPath.row];
    cell.scoreLabel.text = [NSString stringWithFormat:@"%ld", [self.model scoreForIndex:indexPath.row]];
    cell.commentLabel.text = [NSString stringWithFormat:@"%ld", [self.model commentsCountForIndex:indexPath.row]];
    cell.displayDiscountedPrice.text = [self.model displayDiscountedPriceForIndex:indexPath.row];
    cell.tagLabel.text = [NSString stringWithFormat:@"%ld", (long)(indexPath.row + 1) + indexPath.section * self.model.model.pageSize];
    cell.backgroundColor = [UIColor whiteColor];
    
    return cell;
}

#pragma mark - tableView Delegate

#pragma mark - 懒加载
- (UITableView *)tableView {

    if (!_tableView) {
//        CGRect frame = self.view.bounds;
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, DEVICE_SCREEN_WIDTH, DEVICE_SCREEN_HEIGHT - 120 - 64) style:UITableViewStylePlain];
        [self.view addSubview:_tableView];
        
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        [self.tableView registerNib:[UINib nibWithNibName:@"ZXReadAddTableViewCell" bundle:nil] forCellReuseIdentifier:@"RecomSubCell"];
        
        _tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);
        _tableView.rowHeight = 100;
        _tableView.estimatedRowHeight = 100;
        
        // 上拉刷新
        _tableView.mj_header = [CXRefreshStateHeader headerWithRefreshingBlock:^{
            [self.model refreshDataCompletionHandle:^(NSError *error) {
                [self.tableView.mj_header endRefreshing];
                [self.tableView reloadData];
            }];
        }];
    }
    return _tableView;
}

- (ZXReadAddViewModel *)model {

    if (!_model) {
        _model = [[ZXReadAddViewModel alloc] init];
    }
    return _model;
}



@end
